﻿using Furion.CMS.Core;
using Furion.CMS.Core.EventSource;
using Furion.DependencyInjection;
using Furion.EventBus;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Application
{
    /// <summary>
    /// 外部事件触发器
    /// <para>通过Jaina事件总线来触发</para>
    /// </summary>
    public class OuterEventTrigger : IEventSubscriber, ISingleton
    {
        private readonly IOuterEventHub OuterEventHub;

        /// <summary>
        /// 构造函数
        /// </summary>
        public OuterEventTrigger(IOuterEventHub outerEventHub)
        {
            this.OuterEventHub = outerEventHub;
        }

        /// <summary>
        /// 触发外部事件处理程序
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [EventSubscribe(EventIdConsts.TriggerOuterEvent)]
        public async Task TriggerOuterEventHandler(EventHandlerExecutingContext context)
        {
            if (context.Source is OnTriggerOuterEvent notify)
            {
                var currentDateTime = DateTime.Parse($"{DateTime.Now:yyyy-MM-dd HH:mm}");
                var defaultComment = $"{notify.Category.ToString()} {currentDateTime:o}";
                if (string.IsNullOrWhiteSpace(notify.UserId))
                {// 全域广播通知
                    OuterEventHub.GetAll().ForEach(ep =>
                    {
                        ep.Publish(new OuterEventModel
                        {
                            Event = notify.Category.ToString(),
                            Comment = defaultComment,
                            DataObject = notify.Payload
                        });
                    });
                }
                else if (OuterEventHub.Get(notify.UserId) is IOuterEventProvider eventProvider)
                {
                    // 专属消息通知
                    eventProvider.Publish(new OuterEventModel
                    {
                        Event = notify.Category.ToString(),
                        Comment = defaultComment,
                        DataObject = notify.Payload
                    });
                }
            }
            await Task.CompletedTask;
        }

        /// <summary>
        /// 触发多个外部消息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        [EventSubscribe(EventIdConsts.TriggerManyOuterEvent)]
        public async Task TriggerManyOuterEventHandler(EventHandlerExecutingContext context)
        {
            if (context.Source.Payload is IEnumerable<OnTriggerOuterEvent> list)
            {
                foreach (var notify in list)
                {
                    var currentDateTime = DateTime.Parse($"{DateTime.Now:yyyy-MM-dd HH:mm}");
                    var defaultComment = $"{notify.Category.ToString()} {currentDateTime:o}";
                    if (OuterEventHub.Get(notify.UserId) is IOuterEventProvider eventProvider)
                    {
                        // 专属消息通知
                        eventProvider.Publish(new OuterEventModel { Event = notify.Category.ToString(), Comment = defaultComment, DataObject = notify.Payload });
                    }
                }
            }
            await Task.CompletedTask;
        }
    }
}
